<template>

    <div>

        <!--            标题-->
        <el-row class=" border_bottom" type="flex" justify="start">
            <el-col :span="2">
                <h1> 角色管理</h1>
            </el-col>
        </el-row>
<el-row>
    <el-input v-model="currentRole.roleName" style="width: 300px" disabled="disabled">

    </el-input>
<!--    角色id:{{currentRoleId}},角色名称:-->
</el-row>

        <el-row>

            <el-tree
                    :data="data"
                    show-checkbox
                    node-key="menuId"
                    default-expand-all
                    :default-checked-keys="quanxianArr"
                    ref="tree"
                    highlight-current
                    :props="defaultProps">
            </el-tree>

        </el-row>


        <el-row>
            <el-button type="primary" plain @click="getCheckedNodes">修改</el-button>

        </el-row>

    </div>

</template>

<script>
    import jwtDecode from "jwt-decode";
    export default {
        name: "EditRole",
        data(){
            return{
                currentRoleList:[],
                data: [],
                //默认选中
                quanxianArr:[],
                defaultProps: {
                    children: 'subMenu',
                    label: 'menuName'
                },
                currentRole:''
            }
        },
        created() {


            console.log('进来了')
            //获取当前要编辑的角色
            this.currentRole=  JSON.parse(sessionStorage.getItem('currentRole'))
            //获取角色对应的权限
            this.$axios.post('/api/admin/showAuth?roleId='+this.currentRole.roleId).then((res)=>{
                if (res.data.code == 200) {
                    this.currentRoleList=res.data.data
                    console.log('设置角色和拥有权限')
                    console.log(JSON.parse(sessionStorage.getItem('currentRoleList')))
                    console.log('=========================================')
                }
            })
            //获取所有的权限列表
            this.$axios.post('/api/admin/getAuth').then((res)=>{
                // console.log(res);
                if (res.data.code == 200) {
                    this.data=res.data.data
                    // 默认选中
                    this.quanxianArr=this.currentRoleList
                }
            })
        },
        methods:{
            open2() {
                this.$message({
                    message: '权限更新成功！',
                    type: 'success'
                });
            },
            getCheckedNodes() {
                var menuArr=this.$refs.tree.getCheckedNodes()
                var menuIdArr=[]
                menuArr.forEach(function (item) {
                    menuIdArr.push(item.menuId)
                })
                console.log(menuIdArr)
                this.$axios.post('/api//admin/updateAuth?roleId='+this.currentRole.roleId+'&authIds=',[...menuIdArr]).then((res)=>{
                    console.log(res);
                    if (res.data.code==200){
                       this.open2()
                    }
                })
            },
        }
    }
</script>

<style scoped>
    .border_bottom {

        border-bottom: 1px solid grey;
    }
</style>